const { redis } = require("./index");
const zsetName = "videoSet";
//根据视频id以及数字添加对应
// 观看+1 点赞+2 评论+2  收藏+3
const hotInc = async (videoId, num) => {
  const data = await redis.zscore(zsetName, videoId);
  if (data) {
    redis.zincrby(zsetName, num, videoId); //在原来的基础上增加num值
  } else {
    await redis.zadd(zsetName, num, videoId); //新增
  }
};

// 获取前几个热门视频
const getHotInc = async (num) => {
  const hotList = await redis.zrevrange(zsetName, 0, -1, "withscores"); //获取全部带分数的列表
  const dataList = hotList.splice(0, num * 2); //获取指定范围视频
  let hotObj = {};
  for (let i = 0; i <= dataList.length; i++) {
    //整理数据集偶数为key键 奇数为value值
    if (i % 2 == 0) {
      hotObj[[dataList[i]]] = dataList[i + 1];
    }
  }
  return hotObj;
};

module.exports = {
  hotInc,
  getHotInc,
};
